const db = wx.cloud.database()
let app=getApp()
Page({
  data:{
    nickname:''
  },
  async getuserProfile(){
    let res = await wx.getUserProfile({
      desc: '用于完善信息',
    })
    console.log(res)
    let {nickName} =res.userInfo
    console.log(nickName)
    this.setData({
      nickName
    })
  },
  async formsubmit(e){
    console.log(e)
    let {sn,name} = e.detail.value
    console.log(sn,name)
    if(!this.data.nickName){
      wx.showToast({
        title: '请获取微信账号信息',
      })
      return
    }
    if(!sn||!name){
      wx.showToast({
        title: '请输入学号姓名',
      })
      return
    }
    if(!/\d{8}/.test(sn)){
      wx.showToast({
        title: '请输入8位学号',
      })
      return
    }
    let nickName = this.data.nickName
    let res =await wx.cloud.callFunction({
      name:'register',
      data:{
        sn,name,nickName
      }
    })
    console.log('register:',res)
    if(res.result.reg=='ok'){
      app.globalData.user = res.result.user
      wx.showToast({
        title: '绑定成功',
      })
      wx.navigateTo({
        url: '/pages/zhuye/zhuye',
      })
    }else{
      wx.showModal({
        cancelCorlor:'cancelColor',
        title:res.result.reg,
        content:res.result.errMsg

      })
    }
  }
})